package com.zl.codereview;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.zl.codereview.review.ReviewContent;
import com.zl.codereview.util.IdeaUtils;
import com.zl.codereview.util.ObjectUtils;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class MyFileOpenListener implements FileEditorManagerListener {
    @Override
    public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
        Project project = source.getProject();

        // 打开的是Java文件，执行你的处理逻辑
        processJavaFileOpening(project, file);
    }

    private void processJavaFileOpening(Project project, VirtualFile file) {
        // 在这里添加你的代码来处理Java文件打开时的操作，例如在行号区添加标记
        //代码评审路径
        String codeReviewPath = IdeaUtils.codeReviewPath(project,file);
        List<ReviewContent> reviewContents  = IdeaUtils.parseObject(codeReviewPath);
        if(!ObjectUtils.isEmpty(reviewContents)){
            for(ReviewContent content : reviewContents){
                if(file.getPath().equals(content.getPath())) {
                    IdeaUtils.addGutterIcon(project,content.getAuthor()+": "+content.getContent(), content.getNumber(), content.getStatus());
                }
            }
        }
    }

    public static void registerListener() {
        ApplicationManager.getApplication().getMessageBus().connect()
                .subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new MyFileOpenListener());
    }
}
